import 'package:flutter/material.dart';
import 'package:god_logistics/pages/home/waitingItem.dart';
import 'package:god_logistics/utils/http.dart';
import 'package:god_logistics/utils/showToast.dart';
import 'package:god_logistics/utils/tokenManage.dart';

class Waiting extends StatefulWidget {
  const Waiting({super.key});

  @override
  State<Waiting> createState() => _WaitingState();
}

class _WaitingState extends State<Waiting> {
  var queryParams = {'page': 1, 'pageSize': 10, 'status': 1};
  List waitingList = [];
  // 获取任务列表
  getData() async {
    var res = await http.get('/driver/tasks/list', params: queryParams);
    // ToastUtil.showInfo(res);
    if (res['code'] == 200) {
      setState(() {
        // ToastUtil.showError('${res['data']['items']}}');
        waitingList = res['data']['items'];
        // ToastUtil.showError('${waitingList.length} ${waitingList}');
      });
    }
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    getData();
  }

  // 刷新数据
  Future<void> _onrefresh() async {
    waitingList = [];
    getData();
  }

  bool isloading = false;

  // 获取更多数据
  Future<void> _loadMore() async {
    if (isloading) return;
    setState(() {
      isloading = true;
    });
    var queryParams2 = queryParams;
    queryParams2['page'] = queryParams['page']! + 1;
    var res = await http.get('/driver/tasks/list', params: queryParams2);
    if (res['code'] == 200) {
      setState(() {
        waitingList.addAll(res['data']['items']);
      });
    }
    isloading = false;
  }

  @override
  Widget build(BuildContext context) {
    return RefreshIndicator(
      onRefresh: _onrefresh,
      child: ListView.builder(
        itemCount: waitingList.length + 1,
        itemBuilder: (context, index) {
          if (index < waitingList.length) {
            return Waitingitem(waitingItem: waitingList[index]);
          } else {
            return Padding(
              padding: const EdgeInsets.symmetric(vertical: 32.0),
              child: Center(
                child:
                    !isloading
                        ? ElevatedButton(
                          onPressed: _loadMore,
                          child: Text("加载更多"),
                        )
                        : CircularProgressIndicator(),
              ),
            );
          }
        },
      ),
    );
  }
}


// ListView(
//         scrollDirection: Axis.vertical,
//         children:
//             waitingList.map((item) {
//               return Waitingitem(waitingItem: item);
//             }).toList(),
//       )